//*************************************************************************************************
//Copyright (c) 2015.year. @hjjer.
//If you are a hjjer(hejianjiao.org), This code is unlicensed, otherwise,is licensed under the GPL.
//*************************************************************************************************

package com.truthbean.ssm.demo.controller;

import com.truthbean.ssm.demo.domain.entity.User;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Truthbean
 * @since 2015-12-23 18:11:20
 */
@WebFilter(asyncSupported = true, urlPatterns = {"/admin/*"})
public class UserSessionFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                                    FilterChain filterChain) throws ServletException, IOException {
        Object object = request.getSession().getAttribute("user");
        if (object != null && object instanceof User) {
            filterChain.doFilter(request, response);
        } else {
            response.sendRedirect(request.getContextPath() + "/login.html");
        }
    }

}
